
public class Encrypt {

    private byte[] cipher;
    private byte[] text;
    private byte[] IV;
    private String type;
    private boolean encrypt;
    private byte[] integrityKey;
    private byte[] encryptionKey;

    public Encrypt(String itype, byte[] encKey, byte[] intKey, byte[] iIV, byte[] input, boolean iencrypt) throws Exception {
        this.cipher = new byte[input.length];
        this.text = new byte[input.length];
        this.IV = new byte[iIV.length];
        this.encryptionKey = new byte[encKey.length];
        this.encryptionKey = encKey;
        this.integrityKey = new byte[intKey.length];
        this.integrityKey = intKey;
        this.encrypt = iencrypt;
        this.IV = iIV;
        this.type = itype;

        if (this.encrypt) {
            this.text = input;
        } else {
            this.cipher = input;
        }


        //do 3DES/CFB
        if (type.equals("CFB")) {
            System.out.println("Encryption Type: 3DES/CFB");

            CFB cfb = new CFB();            
            
            if(this.encrypt){
                cfb.k = this.text.length; // size of input to be encrypted
                this.cipher = cfb.Encrypt(this.encryptionKey, this.IV, this.text);
                System.out.println("Encrypted Text: " + Utils.ToHexString(this.cipher));
            }else{  // decrypt
                 cfb.k = this.cipher.length; // size of input to be encrypted
                 this.text = cfb.Decrypt(this.encryptionKey, this.IV, this.cipher);
                 System.out.println("Decrypted Text: " + Utils.ByteToString(this.text));
            }
           
        } //do 3DES/CBC
        else if (type.equals("CBC")) {
            System.out.println("Encryption Type: 3DES/CBC");

           // byte[] input1 = Utils.GetUserInput("Enter first sequence: ").getBytes();
           // byte[] input2 = Utils.GetUserInput("Enter second sequence: ").getBytes();

            CBC cbc = new CBC();

            this.cipher = cbc.GetMac(this.encryptionKey, this.IV, this.text);
           // byte[] cipher2 = cbc.GetMac(cbc.GetKeyFromString(key), IV, input2);
            System.out.println("MAC for sequence 1: " + Utils.ToHexString(cipher));
        //    System.out.println("MAC for sequence 2: " + Utils.ToHexString(cipher2));

         //   boolean match = Utils.ToHexString(cipher).equals(Utils.ToHexString(cipher2));
        //    if (match) {
       //         System.out.println("The MACs are equal");
       //     } else {
       //         System.out.println("The MACs are NOT equal");
       //     }
        } //do 3DES/PCBC
        else if (type.equals("PCBC")) {
            System.out.println("Encryption Type: 3DES/PCBC");

            PCBC pcbc = new PCBC();

         //   cipher = pcbc.Encrypt(pcbc.GetKeyFromString(key), IV, input);
            System.out.println("Encrypted Text: " + Utils.ToHexString(cipher));

            String i = Utils.GetUserInput("Do you want to modify cipher text? (Y/N)");
            if (i.toLowerCase().equals("y")) {
                cipher[0] = (byte) (cipher[0] ^ 0xFF);
                System.out.println("Modified Encrypted Text: " + Utils.ToHexString(cipher));
            }

            boolean[] b = new boolean[1];
       //     text = pcbc.Decrypt(pcbc.GetKeyFromString(key), IV, cipher, b);
            System.out.println("Decrypted Text: " + Utils.ByteToString(text));

            if (b[0]) {
                System.out.println("Cipher text was NOT modified");
            } else {
                System.out.println("Cipher text was modified");
            }
        } //do RC4
        else if (type.equals("RC4")) {
            
            if(this.encrypt){
                RC4 rc4 = new RC4(this.text, true, this.encryptionKey);
                this.cipher = rc4.getCipherBytes();
            }else{
                RC4 rc4 = new RC4(this.cipher, false, this.encryptionKey);
                this.text = rc4.getPlainBytes();
            }
        }

    }
}
